<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/12/9
 * Time: 17:16
 */

include_once(ROOTDIR.'include/ap.class.php');
//include_once(ROOTDIR.'include/sdk.class.php');

if(empty($_SESSION["userinfo"])){die("<script>top.location.href='index.php'</script>");}

//素材管理
class Fodder_con extends ap
{
    //查询素材、分页？？？
    public function index()
    {
        $userid = $this->userinfo['userid'];
        //每页显示记录条数
        $pagesize = 3;
        //当前页码
        $page = (int)$_GET['page']? (int)$_GET['page'] : 1;
        $pagenum = ($page - 1) * $pagesize;
        $sort[]='g='.$_GET["g"];
        $sort[]='m='.$_GET["m"];
        $sort[]='a='.$_GET["a"];
//        $sort[]='page='.$_GET["page"];

        //分页字符串
        //$pagestr = $this->termpage('apfodder',"where userid={$userid}",$sort,$page,$pagesize);

        //$info = $this->arrdata('*','apfodder',"where userid={$userid}","id DESC","limit {$pagenum},{$pagesize}");
        $info = $this->arrdata('*','apfodder',"where userid={$userid}","msgid DESC");

        //echo '<pre>';
        $data = array();
        foreach ($info as $k => $v)
        {
            $data[$v['msgid']][] = array(
                'id'       => $v['id'],
                'userid'   => $userid,
                'title'    => $v['title'],
                'img_url'  => $v['img_url'],
                'abstract' => $v['abstract'],
                'content'  => $v['content'],
                'time'     => $v['time'],
                'type'     => $v['type'],
            );
        }
        //echo '<pre>';
        //print_r($data);die;
        //$this->smarty->assign("pagestr",$pagestr);
        $this->smarty->assign("fodder",$data);
        $this->smarty->assign("basehost",$this->basehost);
        $this->smarty->display("material.tpl");

    }

    //添加/编辑素材
    public function add()
    {
        //echo '<pre>';
        //print_r($_POST);die;
        //用户ID
        $userid = $this->userinfo['userid'];

        //获取最大msgid
        $maxmsgid = $this->viewdata("max(msgid)","apfodder");
        $msgid = $maxmsgid[0]+1 ? $maxmsgid[0]+1 : 1;

        //单图文
        if(isset($_POST['type']) && $_POST['type'] == 1)
        {
            //print_r($into);die;
            //编辑
            if($_POST['id'])
            {
                $into['title']    = $_POST['title'];
                $into['abstract']    = $_POST['abstract'];
                $into['img_url'] = trim($_POST['img_url'], "\'");
                $into['content'] = $_POST['content'];
                $this->updata('apfodder',$into,"where id={$_POST['id']}");
            }
            else
            {
                //添加
                $into['title']    = $_POST['title'];
                $into['abstract']    = $_POST['abstract'];
                $into['img_url'] = trim($_POST['img_url'], "\'");
                $into['content'] = $_POST['content'];
                $into['userid'] = $userid;
                $into['msgid'] = $msgid;
                $into['type'] = 1;
                $into['time'] = date('Y-m-d H:i:s', time());

                $this->setdata("apfodder",$into);
            }
        }
        else
        {
            //多图文
            if($_POST['id'])
            {
                $ids = $_POST['id'];
                $title = $_POST['title'];
                $img = $_POST['ajaximg'];
                $content = $_POST['intro'];

//                echo '<pre>';
//                print_r($_POST);
                $arr = array();
                for ($i=0; $i <count($title) ; $i++) {
                    $arr[$i]['title'] = $title[$i];
                    $arr[$i]['img_url'] = trim($img[$i], "\'");
                    $arr[$i]['content'] = $content[$i];
                    $arr[$i]['id'] = (int)$ids[$i];
                }
//                echo '<pre>';
//                var_dump($arr);die;
                foreach ($arr as $v)
                {
                    if($v['id'])
                    {
                        $this->updata('apfodder',$v,"where id={$v['id']}");
                    }
                    else
                    {
                        $v['userid'] = $userid;
                        $v['msgid'] = $_POST['msgid'];
                        $v['type'] = 2;
                        $v['time'] = date('Y-m-d H:i:s', time());
                        $this->setdata("apfodder",$v);
                    }
                }
            }
            else
            {
                //添加
                $title = $_POST['title'];
                $img = $_POST['ajaximg'];
                $content = $_POST['intro'];

                $arr = array();
                for ($i=0; $i <count($title) ; $i++) {
                    $arr[$i]['title'] = $title[$i];
                    $arr[$i]['img_url'] = trim($img[$i], "\'");
                    $arr[$i]['content'] = $content[$i];
                    $arr[$i]['userid'] = $userid;
                    $arr[$i]['msgid'] = $msgid;
                    $arr[$i]['type'] = 2;
                    $arr[$i]['time'] = date('Y-m-d H:i:s', time());
                }
                //print_r($arr);
                foreach ($arr as $v)
                {
                    //print_r($v);
                    $this->setdata("apfodder",$v);
                }
            }

        }

        //跳转到列表页面
        header("location:?g=alipay&m=fodder&a=index");exit;
    }

    /**
     * ajax 图片上传
     */
    public function ajaxup(){
        //echo json_encode($_FILES);exit;
        if($_FILES['uploadimg']['error']){
            echo json_encode(array('status'=>0,'msg'=>'上传失败'));exit;
        }
        $uptypes = array(
            'image/jpg',
            'image/jpeg',
            'image/png',
            'image/pjpeg',
            'image/gif',
            'image/bmp',
            'image/x-png'
        );
        if(!in_array($_FILES['uploadimg']['type'],$uptypes)){
            echo json_encode(array('status'=>0,'msg'=>'上传失败,文件类型不正确'));exit;
        }
        if($_FILES['uploadimg']['size']>2097152){
            echo json_encode(array('status'=>0,'msg'=>'上传失败,文件大小不能超过2M'));exit;
        }


        $res = $this->upload($_FILES['uploadimg'],'fodder');
        echo json_encode(array('status'=>1,'msg'=>$res['pic']));exit;
    }

    //显示单图文
    public function single()
    {
        $this->smarty->display("edit_msg_single.tpl");
    }
    //显示多图文
    public function multi()
    {
        $this->smarty->display("edit_msg_multi.tpl");
    }

    //修改素材
    public function edit()
    {
        $msgid = (int)$_GET['msgid'];

        //查询
        $info = $this->arrdata("*","apfodder","where msgid={$msgid}");
        //echo '<pre>';
        //print_r($info);die;
        $cnum = count($info);

        if($cnum == 1)
        {
            $data = array(
                'id'    => $info[0]['id'],
                'title'    => $info[0]['title'],
                'img_url'  => $info[0]['img_url'],
                'abstract' => $info[0]['abstract'],
                'content'  => $info[0]['content'],
            );
            $this->smarty->assign("data",$data);
            $this->smarty->display("edit_msg_single.tpl");
        }
        else
        {
            foreach ($info as $k => $v)
            {
                $data[] = array(
                    'id'    => $v['id'],
                    'title'    => $v['title'],
                    'img_url'  => $v['img_url'],
                    'content'  => $v['content'],
                );

            }
            //echo '<pre>';
            //print_r($data);die;
            $this->smarty->assign("msgid",$msgid);
            $this->smarty->assign("data",$data);
            $this->smarty->display("edit_msg_multi.tpl");
        }

    }

    //删除素材
    public function dele()
    {
        $msgid = $_REQUEST['msgid'];

        $res = $this->deldata('apfodder',"where msgid={$msgid}");
        if($res)
        {
            //
            echo json_encode(array('status' => 1));
        }
    }

    //ajax删除素材
    public function ajaxdel()
    {
        $id = $_POST['id'];

        $res = $this->deldata('apfodder',"where id={$id}");
        if($res)
        {
            //
            echo json_encode(array('status' => 1));
        }
    }






}